// Bloom.fx 

// ###################
// ##### GLOBALS #####

#define SAMPLE_COUNT 15

// #######################
// ##### PARAMENTERS #####

//sampler BaseSampler;

float BloomThreshold;

float2 SampleHorizontalOffsets[SAMPLE_COUNT];
float SampleHorizontalWeights[SAMPLE_COUNT];
float2 SampleVerticalOffsets[SAMPLE_COUNT];
float SampleVerticalWeights[SAMPLE_COUNT];

// #########################
// ##### RENDERTARGETS #####

texture BaseTexture : RENDERCOLORTARGET
< 
    float2 ViewportRatio = { 0.25, 0.25 };
    int MIPLEVELS = 1;
>;
sampler BaseSampler = sampler_state 
{
    texture = <BaseTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = NONE;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

texture SaturateTexture : RENDERCOLORTARGET
< 
    float2 ViewportRatio = { 0.25, 0.25 };
    int MIPLEVELS = 1;
>;
sampler SaturateSampler = sampler_state 
{
    texture = <SaturateTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = NONE;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

texture BlurHTexture : RENDERCOLORTARGET
< 
    float2 ViewportRatio = { 0.25, 0.25 };
    int MIPLEVELS = 1;
>;
sampler BlurHSampler = sampler_state 
{
    texture = <BlurHTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = NONE;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

texture BlurVTexture : RENDERCOLORTARGET
< 
    float2 ViewportRatio = { 0.25, 0.25 };
    int MIPLEVELS = 1;
>;
sampler BlurVSampler = sampler_state 
{
    texture = <BlurVTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = NONE;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

float4 PixelShader(float2 texCoord : TEXCOORD0) : COLOR0
{
	return tex2D(BaseSampler, texCoord);
}

////////////////////////////////////////////////////////
// Saturate Filter
////////////////////////////////////////////////////////

/*float4 Saturate(float2 texCoord : TEXCOORD0) : COLOR0
{
    // Look up the original image color.
    float4 c = tex2D(TextureSampler, texCoord);

    // Adjust it to keep only values brighter than the specified threshold.
    return saturate((c - BloomThreshold) / (1 - BloomThreshold));
}*/

////////////////////////////////////////////////////////
// GaussianBlur Horizontal Filter
////////////////////////////////////////////////////////

float4 GaussianBlurH(float2 texCoord : TEXCOORD0, uniform sampler2D baseSampler) : COLOR0
{
    float4 c = 0;
    
    // Combine a number of weighted image filter taps.
    for (int i = 0; i < SAMPLE_COUNT; i++)
    {
        c += tex2D(baseSampler, texCoord + SampleHorizontalOffsets[i]) * SampleHorizontalWeights[i];
    }
    
    return c;
}

////////////////////////////////////////////////////////
// GaussianBlur Vertical Filter
////////////////////////////////////////////////////////

float4 GaussianBlurV(float2 texCoord : TEXCOORD0, uniform sampler2D tex) : COLOR0
{
    float4 c = 0;
    
    // Combine a number of weighted image filter taps.
    for (int i = 0; i < SAMPLE_COUNT; i++)
    {
        c += tex2D(tex, texCoord + SampleVerticalOffsets[i]) * SampleVerticalWeights[i];
    }
    
    return c;
}

////////////////////////////////////////////////////////
// Combine Filter
////////////////////////////////////////////////////////

float4 Combine(float2 texCoord : TEXCOORD0, uniform sampler2D baseSampler, uniform sampler2D bloomSampler) : COLOR0
{
    // Look up the bloom and original base image colors.
    float4 bloom = tex2D(bloomSampler, texCoord);
    float4 base = tex2D(baseSampler, texCoord);
    
    // Combine the two images.
    return base + bloom;
}

// #######################
// ##### TECHNIQUES ######

technique GaussianBlur
{
    pass Saturate
    {
        PixelShader = compile ps_2_0 PixelShader();
    }
    
    pass GaussianBlurHorizontal
    {
        PixelShader = compile ps_2_0 GaussianBlurH(SaturateSampler);        
    }
    
    pass GaussianBlurVertical
    {
        PixelShader = compile ps_2_0 GaussianBlurV(BlurHSampler);
    }
    
    /*pass Combine
    {
		PixelShader = compile ps_2_0 Combine(TextureSampler, BlurVSampler);
    }*/
}
